﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace Hourglass.Qrobot.Models
{
    public sealed class GroupsManager
    {
        public Dictionary<long, Group> Groups { get; set; }

        public GroupsManager()
        {
            this.Groups = new Dictionary<long, Group>();
        }

        public GroupMember FindMemberByName(string markName, long groupCode)
        {
            if (Groups.ContainsKey(groupCode))
            {
                return Groups[groupCode].FindMemberByName(markName);
            }
            return null;
        }

        public GroupMember FindMemberById(long id, long groupCode)
        {
            if (Groups.ContainsKey(groupCode))
            {
                return Groups[groupCode].FindMemberById(id);
            }
            return null;
        }

        public GroupMember FindMemberByQq(long qq, long groupCode)
        {
            if (Groups.ContainsKey(groupCode))
            {
                return Groups[groupCode].FindMemberByQq(qq);
            }
            return null;
        }

        public Group FindGroupByNumber(long number)
        {
            return Groups.Values.FirstOrDefault(g => g.GroupNumber.Equals(number));
        }
    }

    public sealed class Group
    {
        /// <summary>
        /// 群组ID
        /// </summary>
        public long Id { get; set; }

        public long Code { get; set; }

        /// <summary>
        /// 群号
        /// </summary>
        public long GroupNumber { get; set; }

        public long Class { get; set; }

        /// <summary>
        /// 群组名称
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// 群组等级
        /// </summary>
        public int Level { get; set; }

        /// <summary>
        /// 群主QQ号
        /// </summary>
        public long OwnerQq { get; set; }

        /// <summary>
        /// 群简介
        /// </summary>
        public string Memo { get; set; }

        public int Option { get; set; }

        public int Face { get; set; }

        public long Flag { get; set; }

        public string Fingermemo { get; set; }

        /// <summary>
        /// 群组成员
        /// </summary>
        public Dictionary<long, GroupMember> Members { get; set; }

        /// <summary>
        /// 群组创建时间
        /// </summary>
        public DateTime CreateTime { get; set; }

        public Group()
        {
            this.Members = new Dictionary<long, GroupMember>();
        }

        public Group(long id, long code, long flag, string name) : this()
        {
            this.Id = id;
            this.Code = code;
            this.Flag = flag;
            this.Name = name;
        }

        public GroupMember FindMemberByName(string markName)
        {
            return Members.Values.FirstOrDefault(m => m.GroupCard.Equals(markName));
        }

        public GroupMember FindMemberById(long id)
        {
            if (Members.ContainsKey(id))
            {
                return Members[id];
            }
            return null;
        }

        public GroupMember FindMemberByQq(long qq)
        {
            return Members.Values.FirstOrDefault(m => m.Qq == qq);
        }
    }

    public sealed class GroupMember : IPerson
    {
        private string _nickName;
        private string _groupCard;

        /// <summary>
        /// 用户ID
        /// </summary>
        public long Id { get; set; }

        /// <summary>
        /// 所属群组
        /// </summary>
        public Group BelongGroup { get; set; }

        public long Qq { get; set; }

        /// <summary>
        /// 昵称
        /// </summary>
        public string NickName
        {
            get
            {
                if (string.IsNullOrEmpty(_nickName))
                {
                    return Qq.ToString();
                }
                return _nickName;
            }
            set { _nickName = value; }
        }

        /// <summary>
        /// 群名片
        /// </summary>
        public string GroupCard
        {
            get
            {
                if (string.IsNullOrEmpty(_groupCard))
                {
                    return NickName;
                }
                return _groupCard;
            }
            set { _groupCard = value; }
        }

        /// <summary>
        /// 性别[male/female]
        /// </summary>
        public SmartEnums.Gender Gender { get; set; }

        /// <summary>
        /// 所在地
        /// </summary>
        public Location Location { get; set; }

        public long Flag { get; set; }

        /// <summary>
        /// 登录类型
        /// </summary>
        public SmartEnums.OnlineClientType ClientType { get; set; }

        /// <summary>
        /// 在线状态
        /// </summary>
        public SmartEnums.OnlineStatus Status { get; set; }

        /// <summary>
        /// 会员等级
        /// </summary>
        public int VipLevel { get; set; }

        public GroupMember() { }

        public GroupMember(long id, long flag)
        {
            this.Id = id;
            this.Flag = flag;
        }

        public QqMessage.PersonInfo GetPersonInfo()
        {
            return new QqMessage.PersonInfo
            {
                //Id = Id,
                Qq = Qq,
                NickName = NickName,
                MarkName = GroupCard
            };
        }
    }
}
